//
//  V2TopicModel.m
//  V2EX
//
//  Created by 花菜ChrisCai on 2016/6/12.
//  Copyright © 2016年 Chris. All rights reserved.
//

#import "V2TopicModel.h"
#import "HTMLParser.h"


@implementation V2TopicModel

- (instancetype)initWithUserAvatar:(NSString *)avatar rUrl:(NSString *)rurl ARTTitle:(NSString *)arttitle NUrl:(NSString *)nUrl NName:(NSString *)nname uMber:(NSString *)umember uName:(NSString *)uname crtDate:(NSString *)date lrum:(NSString *)lastrpUM lrun:(NSString *)lastrpUN RPC:(NSString *)replaycount{
    
    self = [super init];
    if (self) {
        self.userAvatar = avatar;
        self.replayUrl = rurl;
        self.articleTitle = arttitle;
        self.nodeUrl = nUrl;
        self.nodeName = nname;
        self.userMember = umember;
        self.userName = uname;
        self.createdDate = date;
        self.lastReplayUserMember = lastrpUM;
        self.lastReplayUserName = lastrpUN;
        self.replayCount = replaycount;
    }
    
    return self;
    
}
@end

/*

@implementation V2TopicList

- (instancetype)initWithArray:(NSArray *)array {
    if (self = [super init]) {
        
        NSMutableArray *list = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dict in array) {
            V2TopicModel *model = [[V2TopicModel alloc] initWithDictionary:dict];
            [list addObject:model];
        }
        
        self.list = list;
        
    }
    
    return self;
}

- (void)dealloc {
    _list = nil;
}

+ (V2TopicList *)getTopicListFromResponseObject:(id)responseObject {
    
    NSMutableArray *topicArray = [[NSMutableArray alloc] init];
    
    @autoreleasepool {
        
        
        NSString *htmlString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSError *error = nil;
        HTMLParser *parser = [[HTMLParser alloc] initWithString:htmlString error:&error];
        
        if (error) {
            NSLog(@"Error: %@", error);
            return nil;
        }
        
        HTMLNode *bodyNode = [parser body];
        
        NSArray *cellNodes = [bodyNode findChildTags:@"div"];
        
        for (HTMLNode *cellNode in cellNodes) {
            if ([[cellNode getAttributeNamed:@"class"] isEqualToString:@"cell item"]) {
                
                //                NSLog(@"%@", cellNode.rawContents);
                
                V2TopicModel *model = [[V2TopicModel alloc] init];
                model.topicCreator = [[V2MemberModel alloc] init];
                model.topicNode = [[V2NodeModel alloc] init];
                
                NSArray *tdNodes = [cellNode findChildTags:@"td"];
                
                NSInteger index = 0;
                for (HTMLNode *tdNode in tdNodes) {
                    
                    //                    NSLog(@"td:\n%@", tdNode.rawContents);
                    NSString *content = tdNode.rawContents;
                    
                    //                    if (index == 0) {
                    if ([content rangeOfString:@"class=\"avatar\""].location != NSNotFound) {
                        
                        HTMLNode *userIdNode = [tdNode findChildTag:@"a"];
                        if (userIdNode) {
                            NSString *idUrlString = [userIdNode getAttributeNamed:@"href"];
                            model.topicCreator.memberName = [[idUrlString componentsSeparatedByString:@"/"] lastObject];
                        }
                        
                        HTMLNode *avatarNode = [tdNode findChildTag:@"img"];
                        if (avatarNode) {
                            NSString *avatarString = [avatarNode getAttributeNamed:@"src"];
                            if ([avatarString hasPrefix:@"//"]) {
                                avatarString = [@"http:" stringByAppendingString:avatarString];
                            }
                            model.topicCreator.memberAvatarNormal = avatarString;
                        }
                    }
                    
                    //                    if (index == 2) {
                    //                        NSLog(@"td:\n%@", tdNode.rawContents);
                    if ([content rangeOfString:@"class=\"item_title\""].location != NSNotFound) {
                        
                        NSArray *aNodes = [tdNode findChildTags:@"a"];
                        
                        for (HTMLNode *aNode in aNodes) {
                            if ([[aNode getAttributeNamed:@"class"] isEqualToString:@"node"]) {
                                NSString *nodeUrlString = [aNode getAttributeNamed:@"href"];
                                model.topicNode.nodeName = [[nodeUrlString componentsSeparatedByString:@"/"] lastObject];
                                model.topicNode.nodeTitle = aNode.allContents;
                                
                            } else {
                                if ([aNode.rawContents rangeOfString:@"reply"].location != NSNotFound) {
                                    model.topicTitle = aNode.allContents;
                                    
                                    NSString *topicIdString = [aNode getAttributeNamed:@"href"];
                                    NSArray *subArray = [topicIdString componentsSeparatedByString:@"#"];
                                    model.topicId = [(NSString *)subArray.firstObject stringByReplacingOccurrencesOfString:@"/t/" withString:@""];
                                    model.topicReplyCount = [(NSString *)subArray.lastObject stringByReplacingOccurrencesOfString:@"reply" withString:@""];
                                    
                                    
                                }
                            }
                        }
                        
                        NSArray *spanNodes = [tdNode findChildTags:@"span"];
                        for (HTMLNode *spanNode in spanNodes) {
                            if ([spanNode.rawContents rangeOfString:@"href"].location == NSNotFound) {
                                model.topicCreatedDescription = spanNode.allContents;
                            }
                            
                            if ([spanNode.rawContents rangeOfString:@"最后回复"].location != NSNotFound || [spanNode.rawContents rangeOfString:@"前"].location != NSNotFound) {
                                
                                NSString *contentString = spanNode.allContents;
                                NSArray *components = [contentString componentsSeparatedByString:@"  •  "];
                                NSString *dateString;
                                
                                if (components.count > 2) {
                                    dateString = components[2];
                                } else {
                                    dateString = [contentString stringByReplacingOccurrencesOfRegex:@"  •  (.*?)$" withString:@""];
                                }
                                
                                NSArray *stringArray = [dateString componentsSeparatedByString:@" "];
                                if (stringArray.count > 1) {
                                    NSString *unitString = @"";
                                    NSString *subString = [(NSString *)stringArray[1] substringToIndex:1];
                                    if ([subString isEqualToString:@"分"]) {
                                        unitString = @"分钟前";
                                    }
                                    if ([subString isEqualToString:@"小"]) {
                                        unitString = @"小时前";
                                    }
                                    if ([subString isEqualToString:@"天"]) {
                                        unitString = @"天前";
                                    }
                                    //                                    unitString = stringArray[1];
                                    dateString = [NSString stringWithFormat:@"%@%@", stringArray[0], unitString];
                                } else {
                                    //                                    dateString = @"just now";
                                    dateString = @"刚刚";
                                }
                                model.topicCreatedDescription = dateString;
                            }
                        }
                        
                    }
                    
                    
                    index ++;
                }
                
                model.state = [[V2TopicStateManager manager] getTopicStateWithTopicModel:model];
                model.cellHeight = [V2TopicListCell heightWithTopicModel:model];
                
                [topicArray addObject:model];
            }
        }
        
    }
    
    V2TopicList *list;
    
    if (topicArray.count) {
        list = [[V2TopicList alloc] init];
        list.list = topicArray;
    }
    
    return list;
    
}
*/

//@end
